package lib.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.DefaultCaret;

import lib.Base.Conversation;
import lib.Base.Message;
import lib.Com.Client;
import lib.Com.Container;
import lib.Common.Path;
import lib.Common.UserId;

import org.swixml.SwingEngine;

public class GuiChatWindow {
	/**
	 * Class which represents one chat conversation
	 */

	private Conversation conversation;
	private JPanel panel;
	private JTextField txtField_message;
	private JTextArea txtArea_msgHistory;
	private JButton btn_close;
	private JButton btn_send;

	private ActionListener handleSend = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			// Check if there is at least one character to send, otherwise do nothing
			if (txtField_message.getText().length() >= 1) {
				Message msg = new Message(UserId.getTimestamp(), e
						.getActionCommand(), txtField_message.getText());
				txtArea_msgHistory.append("\nSEND: " + msg.getText());
				// Create a client object and send the message
				Client client = new Client(Container.knownClients.get(e
						.getActionCommand()), msg);
				client.start();
				txtField_message.setText("");
				conversation.addMsgObj(msg);
			}
		}
	};
	
	// ChatWindow close operation
	private ActionListener handleClose = new ActionListener() {
		public void actionPerformed(ActionEvent e) {

			java.awt.Container c = panel.getParent();
			int i = c.getComponentCount();

			if (i > 0) {
				c.remove(panel);

				if (i == 1) {
					GuiChat.setFrameVisibility(false);
				}
			}
		}
	};

	public GuiChatWindow() {
		this.panel = new JPanel();
		this.panel.setVisible(false);

	}
	
	public GuiChatWindow(SwingEngine engine, Conversation conversation) {
		
		//Render xml document
		try {
			panel = (JPanel) engine.render(new File(Path.GUI_PATH
					+ "gui-chat-window.xml"));
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		this.conversation = conversation;
		Message msg = this.conversation.getLastMsgObj();
		
		// make references to the gui components
		txtField_message = (JTextField) engine.find("txtField_message");
		txtArea_msgHistory = (JTextArea) engine.find("txtArea_msgHistory");

		DefaultCaret caret = (DefaultCaret) txtArea_msgHistory.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

		btn_close = (JButton) engine.find("btn_close");
		btn_send = (JButton) engine.find("btn_send");
		btn_send.setActionCommand(msg.getUserId());

		btn_close.addActionListener(handleClose);
		btn_send.addActionListener(handleSend);
		
		// set panel name
		panel.setName(msg.getUserId());
	}
	
	// Update the chat window with the given String
	public void update(Message obj) {
		String msg = "\nRECEIVED: " + obj.getText();
		txtArea_msgHistory.append(msg);
		conversation.addMsgObj(obj);
	}
	
	// Add chatWindow to container
	public void create() {
		GuiChat.getTabbedPane().add(this.getPanel());
		this.getPanel().setVisible(true);
	}
	
	public JPanel getPanel() {
		return panel;
	}
}